package io.geekidea.springbootplus.system.controller;


import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.entity.SysFavourite;
import io.geekidea.springbootplus.system.service.SysFavouriteService;
import io.geekidea.springbootplus.system.vo.SysFavouriteVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sysFavourite")
@Module("system")
@Api(value = "收藏夹表管理API接口",tags = {"收藏夹表管理"})
public class SysFavouriteController{

    @Resource
    private SysFavouriteService favouriteService;

    @GetMapping("/subscribe/list/{id}")
    @OperationLog(name="根据编号获取收藏夹数据",type= OperationLogType.LIST)
    @ApiOperation(value="根据编号获取收藏夹数据",response = SysFavouriteVo.class)
    public ApiResult<List<SysFavouriteVo>> selectFindFavouriteById(@PathVariable(value ="id")Integer id)throws Exception{
        List<SysFavouriteVo> favourite= favouriteService.findFavouriteById(id);
        return ApiResult.ok(favourite);
    }


    @PostMapping( "/favourite/add")
    @OperationLog(name = "将商品加入收藏夹表中", type = OperationLogType.ADD)
    @ApiOperation(value ="将商品加入收藏夹表中",response = ApiResult. class)
    public ApiResult<Boolean> addToFavorites(@Validated(Add.class) @RequestBody SysFavourite favourite) throws Exception {
        boolean flag = favouriteService.addToFavorites(favourite);
        return ApiResult.result(flag);
    }


    @PostMapping("/favourite/delete/{id}")
    @OperationLog(name = "删除指定收藏商品", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除指定收藏商品", response = ApiResult.class)
    public ApiResult<Boolean> deleteFromSubscribe(@PathVariable("id")Integer id) throws Exception {
        boolean flag = favouriteService.deleteFavorites(id);
        return ApiResult.result(flag);
    }

    @PostMapping("/favourite/delete/batch")
    @OperationLog(name = "批量删除", type = OperationLogType.DELETE_BATCH)
    @ApiOperation(value = "批量删除", response = ApiResult.class)
    public ApiResult<Boolean> deleteAllFromSubscribe(@RequestBody List<Integer> ids) throws Exception {
        boolean flag = favouriteService.deleteAllFavorites(ids);
        return ApiResult.result(flag);
    }

}
